package com.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 1、根据xml配置文件（全局配置文件）创建一个SqlSessionFactory对象，有数据源一些运行环境信息
 * 2、sql映射文件；配置了每一个sql，以及sql的封装规则等
 * 3、将sql映射文件注册在全局配置中
 * 4、写代码：
 *      1）根据全局配置文件得到sqlSessionFactory
 *      2）使用sqlSessionFactory工厂，获取到sqlSession对象使用它来执行CRUD，一个sqlSession就是代表和数据库的一次会话，用完关闭
 *      3）使用sql的唯一标志来告诉mybatis执行哪个sql。sql都是保存在sql映射文件中的
 * (接口式编程)
 *
 * 1.sqlSession代表和数据库一次会话；用完关闭
 * 2.sqlSession和connection一样是非线程安全的，所以不能以共享使用
 * 3.mapper接口没有实现类，但是mybatis会为这个接口生成一个实现类
 *      EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class); 获取一个代理对象
 * 4.两个重要配置文件:
 *      mybatis的全局配置文件：连接池信息、数据库信息、事务管理器等、、系统运行信息
 *      sql映射文件：保存每一个sql语句的映射信息
 *                     将sql映射出来
 *
 */
public class App 
{
    public static void main(String... args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.获取sqlSession实例，能直接执行已经映射的sql语句
        SqlSession sqlSession = sqlSessionFactory.openSession();

        try{
            EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
            //selectMapByLastName(mapper);
            //selectMapById (mapper);
            //selectByLastName(mapper);
            //selectByMap(mapper);
            selectMultiParam(mapper);
            //crud(sqlSession,mapper);

        }finally {
            sqlSession.close();
        }
    }

    private static void selectMapByLastName(EmployeeMapper mapper) {
        Map<Integer, Employee> empByLastName = mapper.getEmpByLastName("%he%");
        for (Integer integer : empByLastName.keySet()) {
            System.out.println(integer+"--->"+empByLastName.toString());
        }
    }

    private static void selectMapById(EmployeeMapper mapper) {
        Map<String, Object> stringObjectMap = mapper.selectMap(7);
        for (String s : stringObjectMap.keySet()) {
            System.out.println(s);
        }
    }

    private static void selectByLastName(EmployeeMapper mapper) {
        List<Employee> like = mapper.getEmpsByLastNameLike("%he%");
        like.forEach(System.out::println);
    }

    private static void selectByMap(EmployeeMapper mapper){
        Map<String,Object> map = new HashMap<>();
        map.put("id","17");
        map.put("lastName","jerry");
        Employee employee = mapper.selectEmployeebyMap(map);
        System.out.println(employee.toString());
    }
    private static void selectMultiParam(EmployeeMapper mapper) {
        Employee jerry = mapper.selectEmployeebyIdAndLastName(2, "jerry");
        System.out.println(jerry.toString());
        Employee employee = mapper.selectEmployeebyId(1);
        System.out.println(employee.toString());

    }

    private static void crud(SqlSession sqlSession, EmployeeMapper mapper) {

            Employee jerry = new Employee(null, "jerry", "as@qq.com", "2",null);

            mapper.addEmp(jerry);
            sqlSession.commit();
            jerry.setGender("9");
            jerry.setId(11);
            mapper.updateEmp(jerry);
            sqlSession.commit();

            mapper.delEmpById(jerry.getId());
        //必须要commit
        sqlSession.commit();
    }
}
